package com.zhn.shortlink.admin.controller;


import com.zhn.shortlink.admin.common.convention.result.Result;
import com.zhn.shortlink.admin.common.convention.result.Results;
import com.zhn.shortlink.admin.dto.req.UserLoginReqDTO;
import com.zhn.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.zhn.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.zhn.shortlink.admin.dto.resp.UserActualRespDTO;
import com.zhn.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.zhn.shortlink.admin.dto.resp.UserRespDTO;
import com.zhn.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


/**
 * 用户管理控制层
 */
@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    /**
     * 根据用户名查用户
     * @param username 用户名
     * @return 用户信息
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUserName(@PathVariable String username){
        UserRespDTO user = userService.getUserByUserName(username);
        return Results.success(user);
    }
    /**
     * 根据用户名查用户
     * @param username 用户名
     * @return 真实用户信息
     */
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUserName(@PathVariable String username){
        UserActualRespDTO user = userService.getActualUserByUserName(username);
        BeanUtils.copyProperties(user, UserActualRespDTO.class);
        return Results.success(user);
    }

    /**
     * 查看用户名是否可用
     * @param username 用户名
     * @return false 代表不可用 true 代表可用
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        boolean b = userService.hasUsername(username);
        return Results.success(b);
    }

    /**
     * 注册用户注册后添加到布隆过滤器
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }

    /**
     * 修改用户
     * @param requestParam
     * @return
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam){
        userService.updateUser(requestParam);
        return Results.success();
    }

    /**
     * 用户登录
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userService.login(requestParam));
    }

    /**
     * 检查用户登陆状态
     * @param token
     * @return true 处于登陆状态 false 登出状态
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam String username,@RequestParam String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     *  登出功能
     * @param username
     * @param token
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam String username,@RequestParam String token){
        userService.logout(username,token);
        return Results.success();
    }
}
